<?php

namespace App\Exports;


use App\Models\ArchiveManage;
use App\Models\ArchiveManageVolume;
use App\Models\LedgerTender;
use App\Models\Project;
use App\Models\Storeroom;
use App\Services\CommonService;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithTitle;

class DocBookExport implements FromCollection, WithColumnWidths, WithStyles, WithCustomStartCell, WithTitle
{
    use Exportable;

    protected $totalRows;
    protected $project;
    protected $year;
    protected $agency;
    protected $storePeriod;
    protected $keyword;

    public function __construct($project, $year, $agency, $storePeriod, $keyword)
    {
        $this->project     = $project;
        $this->year        = $year;
        $this->agency      = $agency;
        $this->storePeriod = $storePeriod;
        $this->keyword     = $keyword;
    }

    public function collection()
    {
        $project     = $this->project;
        $year        = $this->year;
        $agency      = $this->agency;
        $storePeriod = $this->storePeriod;
        $keyword     = $this->keyword;
        $docArchive  = CommonService::getDocArchiveInstance();
        $list        = $docArchive->where('company_id', company_id())
            ->when($project, function ($query) use ($project) {
                $query->where('project', $project);
            })
            ->when($year, function ($query) use ($year) {
                $query->where('year', $year);
            })
            ->when($agency, function ($query) use ($agency) {
                $query->where('agency', $agency);
            })
            ->when($storePeriod, function ($query) use ($storePeriod) {
                $query->where('store_period', $storePeriod);
            })
            ->when($keyword, function ($query) use ($keyword) {
                $query->where('title', 'like', "%{$keyword}%");
            })->get();

        if ($list->isEmpty()) return [];
        $list   = $list->toArray();
        $r_list = [];
        foreach ($list as $val) {
            $tmp                 = [];
            $tmp['year']         = $val['year'];
            $tmp['agency']       = $val['agency'];
            $tmp['store_period'] = $val['store_period'];
            $tmp['box_num']      = $val['box_num'];
            $tmp['piece_num']    = $val['piece_num'];
            $tmp['director']     = $val['director'];
            $tmp['file_num']     = $val['file_num'];
            $tmp['title']        = $val['title'];
            $tmp['c_date']       = $val['c_date'];
            $tmp['page_num']     = $val['page_num'];
            $tmp['remark']       = $val['remark'];
            $tmp['project']      = $val['project'];
            $r_list[]            = $tmp;
        }
        $this->totalRows = count($r_list) + 2;

        return collect($r_list);
    }

    /**
     * 从哪个单元格开始渲染数据，数据集合如 $list
     * @return string
     */
    public function startCell(): string
    {
        return 'A3';
    }

    /**
     * 设置列宽
     * @return array
     */
    public function columnWidths(): array
    {
        return [
            'A' => 10, 'B' => 30, 'C' => 12, 'D' =>10, 'E' => 10, 'F' => 30, 'G' => 30, 'H' => 50, 'I' => 12, 'J' => 12, 'K' => 15, 'L' => 15,
        ];
    }

    /**
     * 样式设置
     * @param Worksheet $sheet
     * @throws PhpSpreadsheetException
     */
    public function styles(Worksheet $sheet)
    {
        $sheet->getDefaultRowDimension()->setRowHeight(42); //设置行高(全部行高)
        $sheet->getStyle('A1:L' . $this->totalRows)->getFont()->setName('宋体'); //宋体
        $sheet->getStyle('A1:L' . $this->totalRows)->getFont()->setSize(12); //大小
        $sheet->getStyle('A1:L' . $this->totalRows)->getAlignment()->setWrapText(true); //自动换行
        $sheet->getStyle('A1:L2')->getFont()->setBold(true);
        $sheet->mergeCells('A1:L1');
        $sheet->setCellValue('A1', '文书档案');
        /**
         * 属性名设置
         */
        $sheet->setCellValue('A2', '年度');
        $sheet->setCellValue('B2', '机构');
        $sheet->setCellValue('C2', '期限');
        $sheet->setCellValue('D2', '盒号');
        $sheet->setCellValue('E2', '件号');
        $sheet->setCellValue('F2', '责任者');
        $sheet->setCellValue('G2', '文号');
        $sheet->setCellValue('H2', '标题');
        $sheet->setCellValue('I2', '日期');
        $sheet->setCellValue('J2', '页数');
        $sheet->setCellValue('K2', '备注');
        $sheet->setCellValue('L2', '所属项目');
        /**
         * 顶部案卷目录样式设置
         */
        $sheet->getStyle('A1:L' . $this->totalRows)->getAlignment()->setVertical('center');//垂直居中
        $sheet->getStyle('A1:L' . $this->totalRows)->getAlignment()->setHorizontal('center');//水平居左
        $sheet->getStyle('G2:H' . $this->totalRows)->getAlignment()->setHorizontal('left');//水平居左
        /**
         * 设置边框
         */
        $styleArray = [
            'borders' => [
                'allBorders' => [
                    'borderStyle' => Border::BORDER_THIN //细边框
                ]
            ]
        ];
        $sheet->getStyle('A1:L' . $this->totalRows)->applyFromArray($styleArray);
    }

    /**
     * sheet名称
     * @return string
     */
    public function title(): string
    {
        return '文书档案';
    }


}
